package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

var adminUsers = gin.H{
	"test":     gin.H{"email": "test", "phone": "110"},
	"benjamin": gin.H{"email": "benjamin", "phone": "120"},
}

func main() {

	r := gin.Default()
	routerGroup := r.Group("/admin", gin.BasicAuth(gin.Accounts{
		// 理论上从数据库查
		"test":     "123",
		"benjamin": "123",
	}))

	// http://127.0.0.1:8080/admin/hello
	routerGroup.GET("/hello", MyHandlerA)

	r.Run()
}

func MyHandlerA(c *gin.Context) {
	user := c.MustGet(gin.AuthUserKey).(string)
	if secret, ok := adminUsers[user]; ok {
		c.JSON(http.StatusOK, gin.H{"user": user, "secret": secret})
	} else {
		c.JSON(http.StatusOK, gin.H{"user": user, "secret": "No secret!"})
	}
}
